package com.jyh.service.impl;



import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyh.dao.UserDao;
import com.jyh.entity.User;
import com.jyh.service.UserService;
import com.jyh.util.JWTUtil;
import com.jyh.vo.LoginVo;
import com.jyh.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/17 16:27
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public Result findAll(int currentPage, int pageSize) {
        Page<User> page = new Page<>(currentPage,pageSize);
        Page<User> page1 = userDao.selectPage(page, null);
        return new Result(2000,"成功",page1);
    }

    @Override
    public Result deleteById(Integer uid) {
        int i = userDao.deleteById(uid);
        if(i!=0){
            return new Result(2000,"成功");
        }
        return new Result(5000,"失败");
    }

    @Override
    public Result updateById(User user) {
        int i = userDao.updateById(user);
        if(i!=0){
            return new Result(2000,"修改成功");
        }
        return new Result(5000,"修改失败");
    }


    @Override
    public Result insert(User user) {
        int i = userDao.insert(user);
        if (i!=0){
            return new Result(2000,"成功");
        }
        return new Result(5000,"失败");
    }

    @Override
    public Result findAll2() {
        List<User> users = userDao.selectList(null);
        return new Result(2000,"查询成功",users);
    }

    @Override
    public Result login(LoginVo loginVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",loginVo.getEmail());
        wrapper.eq("password",loginVo.getPassword());
        User user = userDao.selectOne(wrapper);
        if (user!=null){
            Map<String, Object> map = new HashMap<>();
            map.put("name",loginVo.getEmail());
            String token = JWTUtil.createToken(map);
            return new Result(2000,"登录成功",token);
        }
        return new Result(5000,"登录失败",null);
    }
}
